package Practice;


public class ResizingArrayQueueOfStrings
{
	private String[] strArray = new String[4];
	private int length;
	private int start;
	private int end;

	public boolean isEmpty()
	{
		return start == end;
	}

	private boolean isFull()
	{
		return (end + 1) % strArray.length == start;
	}

	public void enqueue(String item)
	{
		strArray[end++] = item;
		end = end % strArray.length;
		length++;
		if (isFull())
		{
			String[] tempArray = new String[strArray.length * 2];
			for (int i = 0; i < length; i++)
			{
				tempArray[i] = strArray[(start + i) % strArray.length];
			}
			strArray = tempArray;
			start=0;
			end=length;
		}
	}

	public String dequeue()
	{
		String item = strArray[start++];
		start = start % strArray.length;
		length--;
		if (length < strArray.length / 4)
		{
			String[] tempArray = new String[strArray.length / 2];
			for (int i = 0; i < length; i++)
			{
				tempArray[i] = strArray[(start + i) % strArray.length];
			}
			strArray = tempArray;
			start=0;
			end=length;
		}
		return item;
	}
}
